class AlbumController < ApplicationController
  
  def search
    @albums = Album.search params[:search]
  end

  def search_page
  end
  
  def show
  	@album = Album.find(params[:id])
  end
  
  def create_album
    @album = Album.new(params[:album])
    if @album.save
      flash[:notice] = "Album created successfully."
      redirect_to :controller => 'user', :action => 'home'
    else
      flash[:error] = "Album creation failed."
      redirect_to :action => 'create_album_page'
    end
  end
  
  def display
  	if @user.isAdmin == 1
  		redirect_to :action => 'show_admin', :id => params[:id]
  	else
  		redirect_to :action => 'show', :id => params[:id]
  	end
  end
  
  def show_admin
  	if @user.isAdmin == 1
  		@album = Album.find(params[:id])
  	else
      flash[:error] = 'Access Denied.'
      redirect_to :controller => 'user', :action => 'home'
  	end
  end
  
  def destroy
  	@album = Album.find(params[:id])
  	if @user.isAdmin == 1
  	  @album.songs.each do |song|
  	  	Song.delete(song.id)
  	  end
  	  Album.delete(params[:id])
  	  flash[:notice] = 'Delete Successful.'
  	  redirect_to :controller => 'user', :action => 'home'
  	else
      flash[:error] = 'Delete Failed.'
      redirect_to :controller => 'user', :action => 'home'
  	end
  end
  
end
